/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beanKontrol;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import bean.Barang;
import bean.Konsumen;
import bean.Login;
import tools.Koneksi;

/**
 *
 * @author Dionisia
 */
public class beanBarang {

    private Connection conn;

    public beanBarang(Connection koneksi) {
        this.conn = koneksi;
    }

    public static beanBarang getKoneksi() throws SQLException {
        beanBarang kon = new beanBarang(Koneksi.open());

        return kon;
    }

    public void kirim(Barang brg) throws SQLException {



        Connection con = Koneksi.open();
        Statement st = null;

        int thn = brg.getTahun();
        int tanggal = brg.getTanggal();
        String bulan = brg.getBulan();
        String koderesi = brg.getKodeResi();
        String namabarang = brg.getNamaBarang();
        String jenisbarang = brg.getJenisBarang();
        Double tarif = brg.getTarif();
        Double berat = brg.getBerat();
        Double harga = brg.getHarga();
        String tujuan = brg.getKotaTujuan();
        String asal = brg.getKotaAsal();
        String keterangan = brg.getKeterangan();
        Login log = new Login();
        String area = brg.getArea().getLokasi();

        st = con.createStatement();

        String sql = "insert into " + area + " (tanggalKirim,koderesi,namabarang,jenisbarang,tarif,berat,harga,tujuan,asal,keterangan,status) values('"
                + tanggal + "-" + bulan + "-" + thn + "','"
                + koderesi + "','" + namabarang + "','" + jenisbarang + "','" + tarif + "','" + berat + "','" + harga
                + "','" + tujuan + "','" + asal + "','" + keterangan + "','Barang Dikirim')";
        String sql2 = "insert into " + tujuan + " (tanggalKirim,koderesi,namabarang,jenisbarang,tarif,berat,harga,tujuan,asal,keterangan,status) values('"
                + tanggal + "-" + bulan + "-" + thn + "','"
                + koderesi + "','" + namabarang + "','" + jenisbarang + "','" + tarif + "','" + berat + "','" + harga
                + "','" + tujuan + "','" + asal + "','" + keterangan + "','Barang Dikirim')";

        ResultSet rs = null;
        rs = st.executeQuery(sql);
        rs = st.executeQuery(sql2);


        con.close();
    }

    public void simpanDataKonsumen(Konsumen kons) throws SQLException {
        Connection con = Koneksi.open();
        Statement st = null;
        String koderesi = kons.getKoderesi().getKodeResi();
        String namapengirim = kons.getNamaPengirim();
        String alamatpengirim = kons.getAlamatPengirim();
        String tlppengirim = kons.getTlpPengirim();
        String namapenerima = kons.getNamaPenerima();
        String alamatpenerima = kons.getAlamatPenerima();
        String tlppenerima = kons.getTlpPenerima();


        st = conn.createStatement();


        String sql2 = "insert into konsumen (koderesi,namapengirim,alamatpengirim,"
                + "tlppengirim,namapenerima,alamatpenerima,tlppenerima) values ('" + koderesi + "','" + namapengirim
                + "','" + alamatpengirim + "','" + tlppengirim + "','" + namapenerima + "','" + alamatpenerima
                + "','" + tlppenerima + "')";
        ResultSet rs = null;
        rs = st.executeQuery(sql2);
        conn.close();

    }

    public void terimaBarang(Barang brg) throws SQLException {

        Konsumen kons = new Konsumen();
        Connection con = Koneksi.open();
        Statement st = null;
        ResultSet rs = null;
        String koderesi = brg.getKodeResi();

        Login log = new Login();
        String area = brg.getArea().getLokasi();

        st = conn.createStatement();
        String sql = "select * from " + area + " a,konsumen b where a.koderesi= "
                + "b.koderesi and a.koderesi='" + koderesi + "'";

        rs = st.executeQuery(sql);
        while (rs.next()) {
            brg.setTarif(rs.getDouble("tarif"));
            brg.setNamaBarang(rs.getString("namabarang"));
            brg.setKotaTujuan(rs.getString("tujuan"));
            brg.setKotaAsal(rs.getString("asal"));
            brg.setKodeResi(rs.getString("koderesi"));
            brg.setKeterangan(rs.getString("keterangan"));
            brg.setJenisBarang(rs.getString("jenisbarang"));
            brg.setHarga(rs.getDouble("harga"));
            brg.setDate(rs.getString("tanggalKirim"));
            brg.setBerat(rs.getDouble("berat"));
//       
        }
        conn.close();
    }

    public void terimaBarang3(Konsumen kons) throws SQLException {


        Connection con = Koneksi.open();
        Statement st = null;
        ResultSet rs = null;
        String koderesi = kons.getKoderesi().getKodeResi();

        st = conn.createStatement();
        String sql = "select * from konsumen where koderesi='" + koderesi + "'";

        rs = st.executeQuery(sql);
        while (rs.next()) {

            kons.setTlpPengirim(rs.getString("tlppengirim"));
            kons.setTlpPenerima(rs.getString("tlppenerima"));
            kons.setNamaPengirim(rs.getString("namapengirim"));
            kons.setNamaPenerima(rs.getString("namapenerima"));
            kons.setAlamatPengirim(rs.getString("alamatpengirim"));
            kons.setAlamatPenerima(rs.getString("alamatpenerima"));
//       
        }
        conn.close();
    }

    public void update(Barang brg) throws SQLException {

        Konsumen kons = new Konsumen();
        Connection con = Koneksi.open();
        Statement st = null;
        ResultSet rs = null;

        int thn = brg.getTahun();
        int tanggal = brg.getTanggal();
        String bulan = brg.getBulan();
        String koderesi = brg.getKodeResi();
        Login log = new Login();
        String area = brg.getArea().getLokasi();
        String asal = brg.getKotaAsal();

        st = conn.createStatement();
        String sql = "update " + area + " set tanggalterima='" + tanggal + "-" + bulan + "-" + thn + "',"
                + " status='Barang Diterima' where koderesi ='" + koderesi + "'";

        String sql2 = "update " + asal + " set tanggalterima='" + tanggal + "-" + bulan + "-" + thn + "',"
                + " status='Barang Diterima' where koderesi ='" + koderesi + "'";


        st.executeUpdate(sql);
        st.executeUpdate(sql2);
        conn.close();
    }
}
